package quickConnect;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;

import org.quickconnect.ControlObject;

import userInterface.MainFrame;

import Client.ClientController;
import beans.CommunicationBean;
import beans.UserBean;

public class BCOSendLogin implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		// get params
		HashMap parameters = (HashMap) arg0.get(0);
		ClientController controller = MainFrame.mainFrame.getController();
		//JOptionPane.showMessageDialog(null, "credentials before if");
		if(controller.getSocket().isConnected()){
			
			//create login comm bean
			CommunicationBean commBean = new CommunicationBean();
			HashMap params = new HashMap();
			commBean.setCommand("login");
			params.put("username", parameters.get("username"));
			params.put("password", parameters.get("password"));
			commBean.setParameters(params);
			
			try {
				//send the login, and expect the result
				controller.getoStream().writeObject(commBean);
				commBean = (CommunicationBean) controller.getiStream().readObject();
				if(commBean.getParameters().get("user") != null){
					//JOptionPane.showMessageDialog(null, "credentials after if");
					//MainFrame.status.setMessage("Client: Successful Login");
					System.out.println("Client: Successful Login");
					controller.setCurUser((UserBean)commBean.getParameters().get("user"));
					parameters.put("result", new Boolean(true));
					
					return new Boolean(true);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return new Boolean(false);
		} else {
			return new Boolean(false);	
		}
		
	}

}
